﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace tsgl
{
    public partial class borrowerTypeManage : Form
    {
        public borrowerTypeManage()
        {
            InitializeComponent();
        }

        private void borrowerTypeManage_Load(object sender, EventArgs e)
        {
            listViewUserTypeSet();
            initialize(true, false);
            selecteUserFirst();
            cmbMounth.Items.Add("12");
            cmbMounth.Items.Add("24");
            cmbMounth.Items.Add("36");
            cmbMounth.Items.Add("48");
            cmbMounth.Items.Add("60");
            cmbMounth.Items.Add("120");
        }

        private void selecteUserFirst()
        {
            this.txtReaderTypeId.Text = this.listView1.Items[0].SubItems[0].Text.ToString();
            this.txtReaderTypeName.Text = this.listView1.Items[0].SubItems[1].Text.ToString();
            this.nudMaxBookNumber.Value =Convert.ToInt32(this.listView1.Items[0].SubItems[2].Text.ToString());
            this.nudDay.Value =Convert.ToInt32(this.listView1.Items[0].SubItems[3].Text.ToString());
            float f = float.Parse(this.listView1.Items[0].SubItems[4].Text.ToString());
            this.nudCash.Value = Convert.ToDecimal(f);
            this.cmbMounth.Text = this.listView1.Items[0].SubItems[5].Text.ToString();
        }

        private void listViewUserTypeSet()
        {
            listView1.Columns.Add("类型编号");
            listView1.Columns.Add("类型名称");
            listView1.Columns.Add("最大借书数量");
            listView1.Columns.Add("最长借书期限");
            listView1.Columns.Add("超期日罚金");
            listView1.Columns.Add("借书证有效期");
            listView1.View = View.Details;
            listView1.GridLines = true;
            listView1.FullRowSelect = true;
            listView1.HideSelection = false;
            listView1.Columns[0].Width = 80;
            listView1.Columns[1].Width = 100;
            listView1.Columns[2].Width = 100;
            listView1.Columns[3].Width = 100;
            listView1.Columns[4].Width = 100;
            listView1.Columns[5].Width = 100;

            string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    conn.Open();
                    cmd.CommandText="select * from 借阅者类型";
                    cmd.CommandType = CommandType.Text;
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        ListViewItem li = new ListViewItem();
                        li.SubItems[0].Text = reader["类型编号"].ToString();
                        li.SubItems.Add(reader["借阅者类型"].ToString());
                        li.SubItems.Add(reader["最大借书数量"].ToString());
                        li.SubItems.Add(reader["最长借书期限"].ToString());
                        li.SubItems.Add(reader["超期日罚金"].ToString());
                        li.SubItems.Add(reader["借书证有效期"].ToString());
                        listView1.Items.Add(li);
                    }
                }
            }
        }

        private void initialize(Boolean str1,Boolean str2)
        {
            txtReaderTypeId.ReadOnly = str1;
            txtReaderTypeName.ReadOnly = str1;
            nudCash.ReadOnly = str1;
            nudDay.ReadOnly = str1;
            nudMaxBookNumber.ReadOnly = str1;
            cmbMounth.Enabled = str2;
        }

        private void Clearn()
        {
            this.txtReaderTypeId.Text = "";
            this.txtReaderTypeName.Text = "";
            this.nudCash.Value = nudCash.Minimum;
            this.nudDay.Value = nudDay.Minimum;
            this.nudMaxBookNumber.Value = nudMaxBookNumber.Minimum;
            this.cmbMounth.Text = "";
        }

        private void listView1_Click(object sender, EventArgs e)
        {
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txtReaderTypeId.Text = this.listView1.FocusedItem.SubItems[0].Text.ToString();
            this.txtReaderTypeName.Text = this.listView1.FocusedItem.SubItems[1].Text.ToString();
            this.nudMaxBookNumber.Value = Convert.ToInt32(this.listView1.FocusedItem.SubItems[2].Text.ToString());
            this.nudDay.Value = Convert.ToInt32(this.listView1.FocusedItem.SubItems[3].Text.ToString());
            float f =float.Parse(this.listView1.FocusedItem.SubItems[4].Text.ToString());
            this.nudCash.DecimalPlaces = 1; 
            this.nudCash.Value =Convert.ToDecimal(f);
            this.cmbMounth.Text = this.listView1.FocusedItem.SubItems[5].Text.ToString();
        }

        public string strFlag;
        private void btnAdd_Click(object sender, EventArgs e)
        {
            strFlag = "Add";
            initialize(false,true);
            this.txtReaderTypeId.ReadOnly = true;
            this.txtReaderTypeName.Focus();
            Clearn();
            this.btnSave.Enabled = true;
            this.btnCancel.Enabled = true;
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            strFlag = "Edit";
            initialize(false, true);
            this.txtReaderTypeId.ReadOnly = true;
            this.txtReaderTypeName.Focus();
            this.btnSave.Enabled = true;
            this.btnCancel.Enabled = true;
        }

        public int readerTypeId;
        private void btnSave_Click(object sender, EventArgs e)
        {
            string readerName = this.txtReaderTypeName.Text.Trim().ToString();
            decimal maxBookNumber = this.nudMaxBookNumber.Value;
            decimal day = this.nudDay.Value;
            decimal cash = this.nudCash.Value;
            string mounth = this.cmbMounth.Text.Trim().ToString();
            if(strFlag=="Add")
            {
                    bookDbClass.ExecuteNonQurey("insert into 借阅者类型(借阅者类型,最大借书数量,最长借书期限,超期日罚金,借书证有效期) values(@b,@c,@d,@e,@f)",new SqlParameter("b",readerName),new SqlParameter("c",maxBookNumber),new SqlParameter("d",day),new SqlParameter("e",cash),new SqlParameter("f",mounth));
                    this.listView1.Clear();
                    listViewUserTypeSet();
                    selecteUserFirst();
                    initialize(true,false);
                    MessageBox.Show("添加成功！","提示信息");
                    this.btnCancel.Enabled = false;
                    this.btnSave.Enabled = false;
            }
            if (strFlag == "Edit")
            {
                readerTypeId = Convert.ToInt32(this.txtReaderTypeId.Text.Trim().ToString());
                bookDbClass.ExecuteNonQurey("update 借阅者类型 set 借阅者类型=@b,最大借书数量=@c,最长借书期限=@d,超期日罚金=@e,借书证有效期=@f where 类型编号=@a", new SqlParameter("b", readerName), new SqlParameter("c", maxBookNumber), new SqlParameter("d", day), new SqlParameter("e", cash), new SqlParameter("f", mounth), new SqlParameter("a", readerTypeId));
                this.listView1.Clear();
                listViewUserTypeSet();
                selecteUserFirst();
                initialize(true, false);
                MessageBox.Show("修改成功！", "提示信息");
                this.btnCancel.Enabled = false;
                this.btnSave.Enabled = false;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //int Index = 0;

            //if (this.listView1.SelectedItems.Count > 0)//判断listview有被选中项
            //{
            //    Index = this.listView1.SelectedItems[0].Index;//取当前选中项的index,SelectedItems[0]这必须为0
            //    listView1.Items[Index].Remove();
            //}
            if (MessageBox.Show("你确认删除此数据吗？", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                readerTypeId = Convert.ToInt32(this.txtReaderTypeId.Text.Trim().ToString());
                bookDbClass.ExecuteNonQurey("delete from 借阅者类型 where 类型编号=@a", new SqlParameter("a", readerTypeId));
                this.listView1.Clear();
                listViewUserTypeSet();
                selecteUserFirst();
                initialize(true, false);
                MessageBox.Show("删除成功！", "提示信息");
                this.btnCancel.Enabled = false;
                this.btnSave.Enabled = false;
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.listView1.Clear();
            listViewUserTypeSet();
            selecteUserFirst();
            initialize(true, false);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}